from collections import namedtuple

# from functools import reduce, partial, partialmethod
from itertools import takewhile
from random import randint


def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        newkeywords.update(fkeywords)
        return func(
            *(newfunc.leftmost_args + fargs + newfunc.rightmost_args), **newkeywords
        )

    newfunc.func = func
    args = iter(args)
    newfunc.leftmost_args = tuple(takewhile(lambda v: v != Ellipsis, args))
    newfunc.rightmost_args = tuple(args)
    newfunc.keywords = keywords
    return newfunc


class Deck:
    """The
    >>> deck = Deck(range(1,14))
    >>> for card in deck:
    ...   print(card, end=',')
    1,2,3,4,5,6,7,8,9,10,11,12,13,
    >>> for i in gen_deck(): print(i, end=',')
    1,2,3,4,5,6,7,8,9,10,11,12,13,
    >>> for i in gen_deck_iter(): print(i, end=',')
    1,2,3,4,5,6,7,8,9,10,11,12,13,
    >>> for i in gen_deck_iter_ho(): print(i, end=',')
    1,2,3,4,5,6,7,8,9,10,11,12,13,
    """

    def __init__(self, iterable):
        self.iterable = iterable

    def __getitem__(self, index):
        return self.iterable[index]


def gen_deck():
    yield from Deck(range(1, 14))  # 返回的是迭代器,不能拆解测试,TODO 通过ipython-yf 代理


def gen_deck_iter():
    toiter = iter(Deck(range(1, 14)))  # callable iterator <iterator object of Iterator>
    # print(type(toiter).__mro__)  # (<class 'iterator'>, <class 'object'>)
    for method in dir(toiter):
        # print(method)
        pass
    return toiter


def gen_deck_iter_ho():
    deck = Deck(range(1, 14))
    toiter = iter(deck)  # callable iterator <iterator object of Iterator>
    if deck == toiter:
        print("unreachable: deck is iterable ,but not iter itself")
    cp = toiter
    toiter = iter(toiter)
    if cp != toiter:
        print("unreachable: iterator reture itself")
    # __iter__ __next__
    # print(type(toiter).__mro__)  # (<class 'iterator'>, <class 'object'>)
    for method in dir(toiter):
        # print(method)
        pass
    return toiter


if __name__ == "__main__":
    import doctest

    print("yieldfrom 测试开始")
    (ff, tt) = doctest.testmod()
    print("测试结束:统计-失败{},总计{}".format(ff, tt))
    print("√" * tt if tt > 0 and not ff else "无测试" if not ff else "×" * ff)
